package globalinfo.core.model;

import java.util.Set;
import java.util.TreeSet;

/**
 * @author odupouy
 *
 * @param <T>
 */
public abstract class BaseNamer<T> implements Namer<T> {
	
	private Set<ObjectListener<? super T>> listeners = new TreeSet<ObjectListener<? super T>>();

	/**
	 * @param object
	 */
	protected void warnAddition(T object) {
		for (ObjectListener<? super T> l : listeners) {
			l.objectAdded(object);
		}
	}

	/**
	 * @param object
	 */
	protected void warnRemoval(T object) {
		for (ObjectListener<? super T> l : listeners) {
			l.objectRemoved(object);
		}
	}

	/**
	 * @param oldObj
	 * @param newObj
	 */
	protected void warnSubstitution(T oldObj, T newObj) {
		for (ObjectListener<? super T> l : listeners) {
			l.objectReplaced(oldObj, newObj);
		}
	}

	@Override
	public void addObjectListener(ObjectListener<? super T> l) {
		listeners.add(l);
	}

	@Override
	public void removeObjectListener(ObjectListener<? super T> l) {
		listeners.remove(l);
	}

}
